package net.schmurgon.gwt.ui;

import com.google.gwt.app.place.AbstractRecordEditActivity;
import com.google.gwt.app.place.PlaceController;
import com.google.gwt.app.place.RecordEditView;
import com.google.gwt.requestfactory.shared.Receiver;
import com.google.gwt.requestfactory.shared.RecordRequest;
import com.google.gwt.requestfactory.shared.RequestObject;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.valuestore.shared.SyncResult;
import com.google.gwt.valuestore.shared.Value;
import net.schmurgon.gwt.request.SeriesRecord;
import net.schmurgon.gwt.request.CourseRecord;
import net.schmurgon.gwt.request.ApplicationRequestFactory;
import net.schmurgon.gwt.scaffold.generated.SeriesEditView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * An activity that requests all info on an Series, allows the user to edit
 * it, and persists the results.
 */
public class SeriesEditActivity extends AbstractRecordEditActivity<SeriesRecord> {
	private static RecordEditView<SeriesRecord> defaultView;

	private static RecordEditView<SeriesRecord> getDefaultView() {
		if (defaultView == null) {
			defaultView = new SeriesEditView();
		}
		return defaultView;
	}

	private final ApplicationRequestFactory requests;

	/**
	 * Creates an activity that uses the default singleton view instance.
	 */
	public SeriesEditActivity(SeriesRecord proxy, ApplicationRequestFactory requests, PlaceController placeController, boolean creating) {
		this(getDefaultView(), proxy, requests, placeController, creating);
	}

	/**
	 * Creates an activity that uses its own view instance.
	 */
	public SeriesEditActivity(RecordEditView<SeriesRecord> view, SeriesRecord proxy, ApplicationRequestFactory requests, PlaceController placeController, boolean creating) {
		super(view, proxy, SeriesRecord.class, creating, requests, placeController);
		this.requests = requests;
	}

    @Override
    public void start(Display display, EventBus eventBus) {
    
    
    getSeriesEditView().setLongCoursePickerValues(Collections.<CourseRecord> emptyList());

    requests.courseRequest().findCourseEntries(0, 50).with(
        net.schmurgon.gwt.scaffold.generated.CourseRecordRenderer.instance().getPaths()).fire(
            new Receiver<List<CourseRecord>>() {
                public void onSuccess(List<CourseRecord> response,
                    Set<SyncResult> syncResults) {
                    List<CourseRecord> values = new ArrayList<CourseRecord>();
                    values.add(null);
                    values.addAll(response);
                    getSeriesEditView().setLongCoursePickerValues(values);
            }
    });
    
    getSeriesEditView().setShortCoursePickerValues(Collections.<CourseRecord> emptyList());

    requests.courseRequest().findCourseEntries(0, 50).with(
        net.schmurgon.gwt.scaffold.generated.CourseRecordRenderer.instance().getPaths()).fire(
            new Receiver<List<CourseRecord>>() {
                public void onSuccess(List<CourseRecord> response,
                    Set<SyncResult> syncResults) {
                    List<CourseRecord> values = new ArrayList<CourseRecord>();
                    values.add(null);
                    values.addAll(response);
                    getSeriesEditView().setShortCoursePickerValues(values);
            }
    });
    
        super.start(display, eventBus);
    }


	@Override
	protected RecordRequest<SeriesRecord> getFindRequest(Value<Long> id) {
	    return requests.seriesRequest().findSeries(id);
	}

	protected RequestObject<Void> getPersistRequest(SeriesRecord record) {
	    return requests.seriesRequest().persist(record);
	}

    private SeriesEditView getSeriesEditView() {
        return ((SeriesEditView) getView());
    }
}
